package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.efounder.chat.http.GetHttpUtil;
import com.efounder.chat.service.MessageService;
import com.efounder.chat.service.OSPService;
import com.efounder.chat.service.SystemInfoService;
import com.efounder.chat.utils.ServiceUtils;
import com.efounder.constant.EnvironmentVariable;
import com.pansoft.openplanet.R;

/**
 * 单纯聊天列表
 * @author wang
 */
public class BCChatListActivity extends BaseActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, BCChatListActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assets_activity_chat_list);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(R.string.tab_menu_message);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startMsgService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GetHttpUtil.cannelRequest();
    }

    /**
     * 开启服务, 在chatSwipeMenu聊天列表fragment中，onDestroy的时候会销毁service，在这里启动一下
     */
    private void startMsgService() {
        //！！！启动服务(再onResume中启动，防止这种情况：Activity处于前台，service先于Activity被回收掉)
        try {
            if (!ServiceUtils.isServiceRunning(getApplicationContext(), MessageService.class.getCanonicalName())) {
                startService(new Intent(this, MessageService.class));
            }

            if (!ServiceUtils.isServiceRunning(getApplicationContext(), SystemInfoService.class.getCanonicalName())) {
                String userId = EnvironmentVariable.getUserID();
                if (userId != null && !"".equals(userId)) {
                    startService(new Intent(this, SystemInfoService.class));
                }
            }
            if (!ServiceUtils.isServiceRunning(getApplicationContext(), OSPService.class.getCanonicalName())) {
                startService(new Intent(this, OSPService.class));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
